package system

import (
	"github.com/gin-gonic/gin"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/service/system"
	"likeadmin/core"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/middleware"
	"likeadmin/util"
)

var UserGroup = core.Group("/system/user", newUserHandler, regUser, middleware.TokenAuth())

func newUserHandler(srv system.IUserService) *userHandler {
	return &userHandler{srv: srv}
}

func regUser(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *userHandler) {
		rg.GET("/list", handle.list)
		rg.GET("/detail", handle.detail)
		rg.POST("/edit", handle.edit)
		rg.POST("/adjustMoney", handle.adjustMoney)

	})
}

type userHandler struct {
	srv system.IUserService
}

// 用户列表
func (uh userHandler) list(c *gin.Context) {
	var page request.PageReq
	var userListReq req.UserListReq

	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
		return
	}
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &userListReq)) {
		return
	}

	res, err := uh.srv.List(c, page, userListReq)

	response.CheckAndRespWithData(c, res, err)
}

// detail user详情
func (uh userHandler) detail(c *gin.Context) {
	var detailReq req.UserDetailReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &detailReq)) {
		return
	}
	res, err := uh.srv.Detail(c, detailReq.Id)

	response.CheckAndRespWithData(c, res, err)
}

// detail user详情
func (uh userHandler) edit(c *gin.Context) {
	var userEditReq req.UserEditReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &userEditReq)) {
		return
	}
	response.CheckAndResp(c, uh.srv.EditField(c, userEditReq))
}

// adjustMoney 余额操作
func (uh userHandler) adjustMoney(c *gin.Context) {
	var adjustMoneyReq req.AdjustMoneyReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &adjustMoneyReq)) {
		return
	}
	println(adjustMoneyReq.Id)
	response.CheckAndResp(c, uh.srv.AdjustMoney(c, adjustMoneyReq))
}
